//
//  HomScrollCollectionReusableView.m
//  WuShiCity
//
//  Created by 王文晓 on 16/7/8.
//  Copyright © 2016年 chengjie. All rights reserved.
//

#import "HomScrollCollectionReusableView.h"
#define KWidth self.frame.size.width
#define KHeight self.frame.size.height


@interface HomScrollCollectionReusableView ()<UIScrollViewDelegate>
@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, assign)NSInteger arrayCount;
@property (nonatomic, strong)UIPageControl *pageControl;


@end



@implementation HomScrollCollectionReusableView

- (void)initWithFrame:(CGRect)frame titlesArray:(NSArray *)titlesArr btnsArray:(NSArray *)btnArray{
    
    
    self.arrayCount = titlesArr.count;
    [self layoutSubviewsWithTitlesArray:titlesArr btnsArray:btnArray];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.contentOffset = CGPointMake(0, 0);
    self.scrollView.delegate = self;
}


- (void)layoutSubviewsWithTitlesArray:(NSArray *)titlesArr btnsArray:(NSArray *)btnArray{
    
    if (titlesArr.count <= 6) {
        
        for (int i = 0; i < btnArray.count; i++) {
            
            if (i%2 == 0) {
 
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                [btn setBackgroundImage:btnArray[i] forState:UIControlStateNormal];
                [btn setFrame:CGRectMake(KWidth*(i)/(btnArray.count), 0, KWidth/(btnArray.count/2), KHeight/2-22)];
                btn.tag = 1000+i;
                [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
                
                UILabel *titleName = [[UILabel alloc] initWithFrame:CGRectMake(KWidth*i/titlesArr.count, KHeight/2-22, KWidth/titlesArr.count/2, 15)];
                titleName.text = titlesArr[i];
                titleName.textAlignment = NSTextAlignmentCenter;
                [self addSubview:btn];
                [self addSubview:titleName];
            }else {

                UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
                [btn1 setBackgroundImage:btnArray[i] forState:UIControlStateNormal];
                [btn1 setFrame:CGRectMake(KWidth*(i-1)/(btnArray.count), KHeight/2, KWidth/(btnArray.count/2), KHeight/2-22)];
                btn1.tag = 2000+i;
                [btn1 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
                
                UILabel *titleName1 = [[UILabel alloc] initWithFrame:CGRectMake(KWidth*(i-1)/titlesArr.count, KHeight-22, KWidth/titlesArr.count/2, 15)];
                titleName1.text = titlesArr[i];
                titleName1.textAlignment = NSTextAlignmentCenter;

                [self addSubview:btn1];
                [self addSubview:titleName1];
            }
        }
    }else {
        
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight)];
        self.scrollView.contentSize = CGSizeMake(self.frame.size.width*(btnArray.count/6+1), self.frame.size.height);
        
        for (int i = 0; i < btnArray.count; i++) {
            
            if (i%2 == 0) {
                
//                UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
//                [btn setTitle:btnArray[i] forState:UIControlStateNormal];
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                [btn setBackgroundImage:btnArray[i] forState:UIControlStateNormal];
                [btn setFrame:CGRectMake(KWidth*i/6, 0, KWidth/3, KHeight/2-22)];
                btn.tag = 1000+i;
                [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
                UILabel *titleName = [[UILabel alloc] initWithFrame:CGRectMake(KWidth*i/6, KHeight/2-22, KWidth/3, 15)];
                titleName.text = titlesArr[i];
                titleName.textAlignment = NSTextAlignmentCenter;
                [self.scrollView addSubview:btn];
                [self.scrollView addSubview:titleName];

            }else {
                
//                UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
//                [btn1 setTitle:btnArray[i] forState:UIControlStateNormal];
                UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
                [btn1 setBackgroundImage:btnArray[i] forState:UIControlStateNormal];
                [btn1 setFrame:CGRectMake(KWidth*(i-1)/6, KHeight/2, KWidth/3, KHeight/2-22)];
                btn1.tag = 1000+i;
                [btn1 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
                UILabel *titleName1 = [[UILabel alloc] initWithFrame:CGRectMake(KWidth*(i-1)/6, KHeight-22, KWidth/3, 15)];
                titleName1.text = titlesArr[i];
                titleName1.textAlignment = NSTextAlignmentCenter;
                [self.scrollView addSubview:btn1];
                [self.scrollView addSubview:titleName1];
            }
        }
        [self addSubview:self.scrollView];
    }
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, KHeight-20, self.frame.size.width, 20)];
    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = btnArray.count/6;
    [self addSubview:self.pageControl];
    self.pageControl.tintColor = [UIColor brownColor];
    self.pageControl.pageIndicatorTintColor = [UIColor brownColor];
    self.pageControl.pageIndicatorTintColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [self.pageControl addTarget:self action:@selector(selectImage) forControlEvents:UIControlEventValueChanged];
}

- (void)btnAction:(UIButton *)sender {
    
    NSLog(@"button=%ld",sender.tag-1000);
}

//- (void)selectImage {
//    
//    CGPoint point = self.scrollView.contentOffset;
//    point.x = point.x * (self.pageControl.currentPage + 1);
//    [self.scrollView setContentOffset:point animated:YES];
//}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

//    NSInteger sumPage = scrollView.contentSize.width/self.frame.size.width;
    NSInteger sunPage = scrollView.contentOffset.x/self.frame.size.width;
    if (scrollView.contentOffset.x == 0) {
        
//        [scrollView setContentOffset:CGPointMake(self.frame.size.width*(sumPage-2), 0) animated:NO];
        self.pageControl.currentPage = sunPage+1;
    }
    if (scrollView.contentOffset.x == (sunPage-1)*self.frame.size.width) {
        
//        [scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
        self.pageControl.currentPage = 0;
    }
    //    self.pageControl.currentPage = sumPage;
}


@end
